﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApFLEXFrameValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CApFLEXFrameValidator : ITxValidator
{
  private readonly ICApFLEXFrameSelection m_selectionFrame;
  private readonly CApCableScopeStatus m_cableScopeStatus;

  internal CApFLEXFrameValidator(
    ICApFLEXFrameSelection selectionFrame,
    CApCableScopeStatus cableScopeStatus)
  {
    this.ValidateEmptyName = true;
    this.ShowError = true;
    this.m_selectionFrame = selectionFrame ?? throw new ArgumentNullException(nameof (selectionFrame));
    this.m_cableScopeStatus = cableScopeStatus ?? throw new ArgumentNullException(nameof (cableScopeStatus));
  }

  internal bool ShowError { get; set; }

  internal bool ValidateEmptyName { get; set; }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      if (objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0)
      {
        string errorMessage = (string) null;
        foreach (ITxObject itxObject2 in (Collection<ITxObject>) objectsByName)
        {
          if (this.IsValidObject(itxObject2, out errorMessage))
          {
            itxObject1 = itxObject2;
            break;
          }
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj)
  {
    string text = (string) null;
    try
    {
      text = obj.Name;
    }
    catch (Exception ex)
    {
    }
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    bool flag = false;
    errorMessage = (string) null;
    if (obj != null)
    {
      if (obj is TxFrame locatableObject)
      {
        if (!this.ShowError)
        {
          flag = true;
        }
        else
        {
          flag = true;
          string str1 = this.GetText((ITxObject) locatableObject) ?? "";
          FrameSelectionType frameSelectionType;
          if (this.m_selectionFrame.IsFrameAlreadyUsed((ITxLocatableObject) locatableObject, out frameSelectionType))
          {
            string str2 = "";
            switch (frameSelectionType)
            {
              case FrameSelectionType.StartFrame:
                str2 = CApFLEXStringTable.StartFrameMessage;
                break;
              case FrameSelectionType.EndFrame:
                str2 = CApFLEXStringTable.EndFrameMessage;
                break;
              case FrameSelectionType.PassNearFrame:
                str2 = CApFLEXStringTable.PassNearFrameMessage;
                break;
              case FrameSelectionType.AttachmentFrame:
                str2 = CApFLEXStringTable.AttachmentFrameMessage;
                break;
            }
            errorMessage = string.Format(CApFLEXStringTable.AttachedFrameIsAlreadyUsedMessage, (object) str1, (object) str2);
            flag = false;
          }
          else if (!this.m_selectionFrame.IsFrameScopeValid((ITxLocatableObject) locatableObject))
          {
            flag = false;
            errorMessage = string.Format(CApFLEXStringTable.AttachedFrameDoesNotBelongToCEQMessage, (object) str1, (object) this.m_cableScopeStatus.ParentEquipment.Name);
          }
        }
      }
      else
      {
        string str = this.GetText(obj) ?? "";
        errorMessage = string.Format(CApFLEXStringTable.SelectedFrameIsNotFrameObjectMessage, (object) str);
      }
    }
    else
      errorMessage = CApFLEXStringTable.MissingObjMessage;
    return flag;
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    errorMessage = (string) null;
    bool flag = false;
    if (text == "")
      flag = true;
    else if (this.GetObject(text) != null)
      flag = true;
    else
      errorMessage = !this.DoesExistObjectByName(text) ? CApFLEXStringTable.InvalidFrameNameMessage : string.Format(CApFLEXStringTable.InvalidFrame, (object) text);
    return flag;
  }

  private bool DoesExistObjectByName(string text)
  {
    bool flag = false;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      flag = objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0;
    }
    return flag;
  }

  private string InDoubleQuotes(string text) => $"\"{text}\"";
}
